package gov.nist.ssd.swqb.core;

import java.io.File;
import java.io.IOException;

/**
 * Manage all the configaration files (creation, loading, ...).
 * @author benjamin herbomez
 */
public class ConfigFileManager {

    private String dir;

    public ConfigFileManager(String dir) {
        this.dir = dir;
        this.init();
    }

    /**
     * Create the dir if not exists
     */
    private void init() {
        File file = new File(dir);
        if (!file.exists()) {
            file.mkdir();
        }
    }

    /**
     * Return valid file location. If create is true, the file will be created if not exists.
     * @param fileName
     * @param create
     * @return null if file does not exits, its location in other cases.
     */
    public String getFileLocation(String fileName, boolean create) {
        String fileDir = dir + fileName;
        File file = new File(fileDir);
        if (file.exists()) {
            return fileDir;
        }
        if (create) {
            try {
                file.createNewFile();
                return fileDir;
            } catch (IOException ex) {}
        }
        return null;
    }
}
